﻿using System;
using System.Collections.Generic;
using System.Linq;
using DomainModel;
using Repository.Abstract;

namespace Repository.NHibernate
{
    public class LessonRepository : Repository<Lesson>, ILessonRepository
    {
        public Lesson GetLessonByDateStudentTimetableUnit(DateTime date, User student, TimetableUnit timetableUnit)
        {
            return
                Session.QueryOver<Lesson>().Where(
                    l =>
                        l.Date.ToShortDateString() == date.ToShortDateString() &&
                        l.Student == student &&
                        l.TimetableUnit == timetableUnit)
                    .SingleOrDefault();
        }

        public IEnumerable<Lesson> GetLessonsByGroup(Group group)
        {
            var all = Session.QueryOver<Lesson>().List();
            var result = all.Where(l => l.TimetableUnit.Group.Name == group.Name);
            return result;
        }

        public IEnumerable<Lesson> GetLessonsByGroupAndSubject(Group group, Subject subject)
        {
            var all = Session.QueryOver<Lesson>().List();
            var result = all.Where(l => l.TimetableUnit.Group.Name == group.Name && l.TimetableUnit.Subject.Name == subject.Name);
            return result;
        }

        public IEnumerable<Lesson> GetLessonsBySemesterAndGroupAndSubject(StudySemester semester, Group group, Subject subject)
        {
            var all = Session.QueryOver<Lesson>().List();
            var result = all.Where(l => l.TimetableUnit.StudySemester.Id == semester.Id && l.TimetableUnit.Group.Id == group.Id && l.TimetableUnit.Subject.Id == subject.Id);
            return result;
        }

        public int GetMissedLessonsNumber(StudySemester semester, User student, Subject subject)
        {
            var l1 = Session.QueryOver<Lesson>().Where(l => l.Student == student).List();
            if (l1.Count == 0)
                return 0;

            var l2 = l1.Where(l => l.TimetableUnit.StudySemester.Id == semester.Id && l.TimetableUnit.Subject.Id == subject.Id);

            var result = l2.Count(l => l.Mark == "Н");

            return result;
        }

        public double GetAvgBall(StudySemester semester, User student, Subject subject)
        {
            var l1 = Session.QueryOver<Lesson>().Where(l => l.Student == student).List();
            if (l1.Count == 0)
                return 0;

            var l2 = l1.Where(l => l.TimetableUnit.StudySemester.Id == semester.Id && l.TimetableUnit.Subject.Id == subject.Id).ToList();
            if (l2.Count == 0)
                return 0;

            var l3 = l2.Where(l => l.Mark != "Н" && !string.IsNullOrEmpty(l.Mark)).ToList();
            if (l3.Count == 0)
                return 0;

            var l4 = l3.Select(l => new { Mark = int.Parse(l.Mark) });

            var result = l4.Average(m => m.Mark);

            return result;
        }

        public IEnumerable<Lesson> GetLessonsBySemesterAndStudentAndDateRange(StudySemester semester, User student, DateTime beginDate, DateTime endDate)
        {
            var l1 = Session.QueryOver<Lesson>().Where(l => l.Student == student).List();
            if (l1.Count == 0)
                return new List<Lesson>();

            var l2 =
                l1.Where(
                    l =>
                    l.TimetableUnit.StudySemester.Id == semester.Id && l.Student.Id == student.Id && l.Date > beginDate &&
                    l.Date < endDate);

            return l2;
        }

        public IEnumerable<Lesson> GetLessonsByStudentAndDate(User student, DateTime date)
        {
            var l1 = Session.QueryOver<Lesson>().Where(l => l.Student == student).List();
            if (l1.Count == 0)
                return new List<Lesson>();

            var result = l1.Where(l => l.Date.ToShortDateString() == date.ToShortDateString());

            return result;
        }
    }
}